package com.java.controller.Login;

import com.alibaba.fastjson.JSONArray;
import com.java.pojo.Stores;
import com.java.pojo.User;
import com.java.pojo.UserStyle;
import com.java.service.StoresService;
import com.java.service.UserService;
import com.java.service.UserStyleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
public class Login {

    @Autowired
    private UserStyleService userStyleService;

    @Autowired
    private UserService userService;
    @Autowired
    private StoresService storesService;


    //登录页面
    @RequestMapping("/Log")
    public String log(Model model){
        List<UserStyle> list = userStyleService.getUserStyleList();
        model.addAttribute("userStyleList",list);
        System.out.println("list+"+list);
        return "login/Login";
    }

    //进入注册页面
    @RequestMapping("/reg")
    public String reg(Model model){
        List<UserStyle> list = userStyleService.getUserStyleList();
        List<Stores> stList = storesService.getStList();
        model.addAttribute("userStyleList",list);
        model.addAttribute("stList",stList);
        System.out.println("list+"+list);
        return "login/reg";
    }

    //注册验证json
    @RequestMapping("/reg2")
    @ResponseBody
    public Object show(@RequestParam("name") String name){
        HashMap<String,String> map=new HashMap<>();
        User byName = userService.getUserByName(name);
        if(byName!=null){
            map.put("userCode","exist");
        }else{
            map.put("userCode","no");
        }
        //将map转为json
        return JSONArray.toJSONString(map);
    }

    //验证完成添加user
    @RequestMapping(value = "/regUser2",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String regUser2(User user){
        int i = userService.addUser(user);
        if(i>0){
            return "<script>alert('注册successful');location.href='/Log';</script>";
        }else {
            return "<script>alert('注册lose');location.href='/Log';</script>";
        }
    }


    //验证完成登录***
    @RequestMapping(value = "/loginUser2",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String loginUser2(User user, HttpServletRequest httpServletRequest){
        System.out.println("[]"+user);
        User user1 = userService.getUserByNameAndPwAndSid(user);
        System.out.println("[]"+user1);
        if(user1!=null){
            HttpSession session = httpServletRequest.getSession();
            if(user1.getSid()==1){
                session.removeAttribute("makerAndINOOTUser");
                session.removeAttribute("resUser");
                session.setAttribute("boosUser",user1.getUsername());
            }else if(user1.getSid()==2){
                 session.removeAttribute("boosUser");
                 session.removeAttribute("makerAndINOOTUser");
                session.setAttribute("resUser",user1.getUsername());
            }else if(user1.getSid()==3){
                session.removeAttribute("boosUser");
                session.removeAttribute("resUser");
                session.setAttribute("makerAndINOOTUser",user1.getUsername());
            }
            return "<script>alert('登录successful,欢迎回来!');location.href='/project/center';</script>";
        }else {
            return "<script>alert('密码或者职务不匹配');location.href='/Log';</script>";
        }
    }



    //登录验证json
    @RequestMapping("/log2")
    @ResponseBody
    public Object log2(@RequestParam("name") String name){
        HashMap<String,String> map=new HashMap<>();//集合
        User byName = userService.getUserByName(name);
        if(byName!=null){
            map.put("userCode","exist");
        }else{
            map.put("userCode","no");
        }
        //将map转为json
        return JSONArray.toJSONString(map);
    }


    @RequestMapping("/s9")
    public String s9(){
        return "succ";
    }


    //退出
    @RequestMapping(value = "/exit",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String exit(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.invalidate();
        return "<script>alert('exit successful!');location.href='/Log';</script>";
    }

}
